/*
类型保护  方法
*/
interface Waiters {
  anjiao: boolean;
  say: () => {};
}
interface Technician {
  anjiao: boolean;
  skill: () => {};
}
//  接口属性
// 类型保护   1. 类型断言
function judgeWho(animal: Waiters | Technician) {
  if (animal.anjiao) {
    (animal as Technician).skill();
  } else {
    (animal as Waiters).say();
  }
}
//  类型保护   2. in 语法
function judgeWhoTwo(animal: Waiters | Technician) {
  if ("skill" in animal) {
    animal.skill();
  } else {
    animal.say();
  }
}
// 两个参数(字符串|数值)相加
//  类型保护    3.  typeof 变量 === '类型'
function addOther(first: string | number, second: string | number) {
  // 如果有一个参数是字符串
  if (typeof first === "string" || typeof second === "string") {
    return `${first}${second}`;
  }
  return first + second;
}
//  类型保护     4.  变量  instanceof 类
class NumberObj {
  count: number;
  constructor() {
    this.count = 0;
  }
}
// 两个参数相加(对象和数值对象)
function addObj(first: object | NumberObj, second: object | NumberObj) {
  if (first instanceof NumberObj && second instanceof NumberObj) {
    return first.count + second.count;
  }
  return 0;
}
